{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>SYS_NAME</th>\n",
       "      <th>CWXT_DB:184:C:\\</th>\n",
       "      <th>CWXT_DB:184:D:\\</th>\n",
       "      <th>COLLECTTIME</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>财务管理系统</td>\n",
       "      <td>34270787.33</td>\n",
       "      <td>80262592.65</td>\n",
       "      <td>2014-10-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>财务管理系统</td>\n",
       "      <td>34328899.02</td>\n",
       "      <td>83200151.65</td>\n",
       "      <td>2014-10-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>财务管理系统</td>\n",
       "      <td>34327553.50</td>\n",
       "      <td>83208320.00</td>\n",
       "      <td>2014-10-03</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>财务管理系统</td>\n",
       "      <td>34288672.21</td>\n",
       "      <td>83099271.65</td>\n",
       "      <td>2014-10-04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>财务管理系统</td>\n",
       "      <td>34190978.41</td>\n",
       "      <td>82765171.65</td>\n",
       "      <td>2014-10-05</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  SYS_NAME  CWXT_DB:184:C:\\  CWXT_DB:184:D:\\ COLLECTTIME\n",
       "0   财务管理系统      34270787.33      80262592.65  2014-10-01\n",
       "1   财务管理系统      34328899.02      83200151.65  2014-10-02\n",
       "2   财务管理系统      34327553.50      83208320.00  2014-10-03\n",
       "3   财务管理系统      34288672.21      83099271.65  2014-10-04\n",
       "4   财务管理系统      34190978.41      82765171.65  2014-10-05"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型构建 C盘\n",
    "# 详解BIC方式定信息准则　＋　ARMA \n",
    "import pandas as pd\n",
    "inputfile = 'attrsConstruction.xlsx'\n",
    "\n",
    "data = pd.read_excel(inputfile)\n",
    "df = data.iloc[:len(data)-5]\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\ProgramData\\Anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "D:\\ProgramData\\Anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "D:\\ProgramData\\Anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "D:\\ProgramData\\Anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 192 from C header, got 216 from PyObject\n",
      "  return f(*args, **kwds)\n",
      "D:\\ProgramData\\Anaconda3\\lib\\importlib\\_bootstrap.py:219: RuntimeWarning: numpy.ufunc size changed, may indicate binary incompatibility. Expected 216, got 192\n",
      "  return f(*args, **kwds)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(-0.5635118147156262, 0.8790155702809298, 3, 38, {'1%': -3.6155091011809297, '5%': -2.941262357486514, '10%': -2.6091995013850418}, 859.9976220423233)\n",
      "0.8790155702809298\n",
      "原始序列经过%s阶差分后归于平稳，p值为%s\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "unsupported operand type(s) for %: 'NoneType' and 'tuple'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-7-d867571dfb39>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     14\u001b[0m     \u001b[0madf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mADF\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'CWXT_DB:184:C:\\\\'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdiff\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdiff\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdropna\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;31m#注意，差分后使用ADF检验时，必须去掉空值\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 16\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34mu'原始序列经过%s阶差分后归于平稳，p值为%s'\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m%\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mdiff\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0madf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     17\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'CWXT_DB:184:C:\\\\_adf'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdf\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'CWXT_DB:184:C:\\\\'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdiff\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for %: 'NoneType' and 'tuple'"
     ]
    }
   ],
   "source": [
    "# 第 * 1 * 步--C盘---------平稳性检测\n",
    "#1)平稳性检测 ：判断是否平稳，若不平稳，对其进行差分处理直至平稳\n",
    "# 方法：采用单位根检验（ADF）的方法或者时序图的方法（见数据探索模块）\n",
    "# 注意：其他平稳性检验方法见steadyCheck.py文件\n",
    "from statsmodels.tsa.stattools import adfuller as ADF\n",
    "diff = 0\n",
    "# 判断D盘数据的平稳性，以及确定几次差分后平稳\n",
    "adf = ADF(df['CWXT_DB:184:C:\\\\'])\n",
    "print(adf) \n",
    "\n",
    "while adf[1] >= 0.05 : # adf[1]是p值，p值小于0.05认为是平稳的\n",
    "    print(adf[1])\n",
    "    diff = diff + 1\n",
    "    adf = ADF(df['CWXT_DB:184:C:\\\\'].diff(diff).dropna())#注意，差分后使用ADF检验时，必须去掉空值\n",
    "    \n",
    "print(u'原始序列经过%s阶差分后归于平稳，p值为%s') % (diff, adf[1])\n",
    "df['CWXT_DB:184:C:\\\\_adf'] = df['CWXT_DB:184:C:\\\\'].diff(1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "原始序列为非白噪声序列，对应的p值为：1.06099075081e-08\n",
      "一阶差分序列为白噪声序列，对应的p值为：0.474552255255\n"
     ]
    }
   ],
   "source": [
    "# 第 * 2 * 步--C盘---------白噪声检验\n",
    "# 目的：验证序列中有用信息是否已经被提取完毕，需要进行白噪声检验。若序列是白噪声序列，说明序列中有用信息已经被提取完，只剩随机扰动\n",
    "# 方法：采用LB统计量的方法进行白噪声检验\n",
    "# 若没有通过白噪声检验，则需要进行模型识别，识别其模型属于AR、MA还是ARMA。\n",
    "\n",
    "inputfile2 = 'attrsConstruction.xlsx'\n",
    "data1 = pd.read_excel(inputfile2)\n",
    "data1 = data1.iloc[:len(data1)-5]# 不使用最后五个数据（作为预测参考）\n",
    "\n",
    "# 白噪声检测\n",
    "from statsmodels.stats.diagnostic import acorr_ljungbox\n",
    "\n",
    "[[lb], [p]] = acorr_ljungbox(data1['CWXT_DB:184:C:\\\\'], lags = 1) ## lags是残差延迟个数\n",
    "if p < 0.05:\n",
    "    print (u'原始序列为非白噪声序列，对应的p值为：%s' % p)\n",
    "else:\n",
    "    print (u'原始序列为白噪声序列，对应的p值为：%s' % p)\n",
    "\n",
    "[[lb], [p]] = acorr_ljungbox(data1['CWXT_DB:184:C:\\\\'].diff(1).dropna(), lags = 1)\n",
    "if p < 0.05:\n",
    "    print (u'一阶差分序列为非白噪声序列，对应的p值为：%s' % p)\n",
    "else:\n",
    "    print (u'一阶差分序列为白噪声序列，对应的p值为：%s' % p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "D:\\Anaconda2\\lib\\site-packages\\statsmodels\\base\\model.py:473: HessianInversionWarning: Inverting hessian failed, no bse or cov_params available\n",
      "  'available', HessianInversionWarning)\n",
      "D:\\Anaconda2\\lib\\site-packages\\statsmodels\\base\\model.py:496: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n",
      "  \"Check mle_retvals\", ConvergenceWarning)\n",
      "D:\\Anaconda2\\lib\\site-packages\\statsmodels\\tsa\\tsatools.py:654: RuntimeWarning: invalid value encountered in log\n",
      "  invmacoefs = -np.log((1-macoefs)/(1+macoefs))\n",
      "D:\\Anaconda2\\lib\\site-packages\\statsmodels\\base\\model.py:496: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals\n",
      "  \"Check mle_retvals\", ConvergenceWarning)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             0            1            2            3            4\n",
      "0  1254.084030  1229.484140  1213.260357          NaN  1214.282270\n",
      "1  1197.862668  1201.518205  1204.591406          NaN  1208.042667\n",
      "2  1201.540913  1203.785729  1207.997558  1208.143231  1211.687458\n",
      "3  1204.822657  1209.040855          NaN  1211.813088  1215.288872\n",
      "4  1204.570230  1207.153552  1210.775594  1211.665267  1216.329316\n",
      "0  0    1254.084030\n",
      "   1    1229.484140\n",
      "   2    1213.260357\n",
      "   4    1214.282270\n",
      "1  0    1197.862668\n",
      "   1    1201.518205\n",
      "   2    1204.591406\n",
      "   4    1208.042667\n",
      "2  0    1201.540913\n",
      "   1    1203.785729\n",
      "   2    1207.997558\n",
      "   3    1208.143231\n",
      "   4    1211.687458\n",
      "3  0    1204.822657\n",
      "   1    1209.040855\n",
      "   3    1211.813088\n",
      "   4    1215.288872\n",
      "4  0    1204.570230\n",
      "   1    1207.153552\n",
      "   2    1210.775594\n",
      "   3    1211.665267\n",
      "   4    1216.329316\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# 第 * 3 * 步----------模型识别\n",
    "# 方法：采用极大似然比方法进行模型的参数估计，估计各个参数的值。\n",
    "# 然后针对各个不同模型，采用信息准则方法（有三种：BIC/AIC/HQ)对模型进行定阶，确定p,q参数，从而选择最优模型。\n",
    "# 注意，进行此步时，index需要为时间序列类型\n",
    "# 确定最佳p、d、q的值\n",
    "inputfile3 = 'attrsConstruction.xlsx'\n",
    "data2 = pd.read_excel(inputfile3,index_col='COLLECTTIME')\n",
    "xtest_value=data2['CWXT_DB:184:C:\\\\'][-5:]\n",
    "data2 = data2.iloc[:len(data2)-5]# 不使用最后五个数据（作为预测参考） \n",
    "# print data2\n",
    "xdata2 = data2['CWXT_DB:184:C:\\\\']\n",
    "# print xdata2\n",
    "# ARIMA（p,d,q）中,AR是自回归,p为自回归项数；MA为滑动平均,q为滑动平均项数,d为使之成为平稳序列所做的差分次数(阶数)，由前一步骤知d=1\n",
    "# from statsmodels.tsa.arima_model import ARIMA#建立ARIMA（p,d，q）模型\n",
    "from statsmodels.tsa.arima_model import ARMA #建立ARIMA（p,q）模型\n",
    "\n",
    "# 定阶\n",
    "# 目前选择模型常用如下准则!!!!!\n",
    "# 增加自由参数的数目提高了拟合的优良性，\n",
    "# AIC/BIC/HQ鼓励数据拟合的优良性但是尽量避免出现过度拟合(Overfitting)的情况。所以优先考虑的模型应是AIC/BIC/HQ值最小的那一个\n",
    "#* AIC=-2 ln(L) + 2 k 中文名字：赤池信息量 akaike information criterion (AIC)\n",
    "# * BIC=-2 ln(L) + ln(n)*k 中文名字：贝叶斯信息量 bayesian information criterion (BIC)\n",
    "# * HQ=-2 ln(L) + ln(ln(n))*k hannan-quinn criterion (HQ)\n",
    "\n",
    "# ----------------------------------------------------------\n",
    "pmax = int(len(xdata2)/10) # 一般阶数不超过length/10\n",
    "qmax = int(len(xdata2)/10) # 一般阶数不超过length/10\n",
    "\n",
    "matrix = [] # bic矩阵\n",
    "for p in range(pmax+1):\n",
    "    tmp = []\n",
    "    for q in range(qmax+1):\n",
    "        try:#存在部分为空值，会报错\n",
    "              tmp.append(ARMA(xdata2, (p,q)).fit().bic) #  \n",
    "#             tmp.append(ARIMA(xdata2, (p,1,q)).fit().bic) #  BIC方式\n",
    "#             tmp.append(ARIMA(xdata2, (p,1,q)).fit().aic) #  AIC方式\n",
    "#             tmp.append(ARIMA(xdata2, (p,1,q)).fit().hq) #  HQ方式\n",
    "        except:\n",
    "            tmp.append(None)\n",
    "            \n",
    "    matrix.append(tmp)\n",
    "    \n",
    "matrix = pd.DataFrame(matrix) # 从中可以找出最小值\n",
    "print matrix\n",
    "print matrix.stack()\n",
    "\n",
    "#              0            1            2            3            4\n",
    "# 0  1166.398597  1169.307301  1170.991139  1168.427478  1172.077129\n",
    "# 1  1169.644775          NaN  1170.684719  1171.724685  1175.002346\n",
    "# 2  1172.246538  1170.593519          NaN  1174.762634  1180.268815\n",
    "# 3  1170.473345  1173.422439  1177.114533  1180.531660          NaN\n",
    "# 4  1173.366376  1177.079054  1178.166253          NaN  1181.982648\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前BIC最小的p值与q值分别为：1、0\n",
      "(1, 0)\n",
      "模型ARMA(1,0)符合白噪声检验\n"
     ]
    }
   ],
   "source": [
    "# 第 * 4 * 步--C盘---------模型检验\n",
    "# 确定模型后，需要检验其残差序列是否是白噪声，若不是，说明，残差中还存在有用的信息，需要修改模型或者进一步提取。\n",
    "# 若其残差不是白噪声，重新更换p,q的值，重新确定\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "while 1:\n",
    "    p, q = matrix.stack().idxmin() # 先展平该表格，然后找出最小值的索引位置\n",
    "    print (u'当前BIC最小的p值与q值分别为：%s、%s' % (p,q))\n",
    "    \n",
    "    lagnum = 12 # 残差延迟个数\n",
    "\n",
    "    # 由模型识别可知第一次BIC最小的p值与q值分别为：0、1\n",
    "\n",
    "    arma = ARMA(xdata2, (p,q)).fit() # 建立并训练模型\n",
    "    xdata_pred = arma.predict() # 预测\n",
    "    pred_error = (xdata_pred - xdata2).dropna() # 计算残差\n",
    "\n",
    "    # 白噪声检测\n",
    "    from statsmodels.stats.diagnostic import acorr_ljungbox\n",
    "\n",
    "    lbx, px = acorr_ljungbox(pred_error, lags = lagnum)\n",
    "    h = (px < 0.05).sum() # p值小于0.05，认为是非噪声\n",
    "    if h > 0:\n",
    "        print (u'模型ARMA(%s,%s)不符合白噪声检验' % (p,q))\n",
    "        print ('在BIC矩阵中去掉[%s,%s]组合，重新进行计算' % (p,q))\n",
    "        matrix.iloc[p,q] =  np.nan\n",
    "        arimafail = arma\n",
    "        continue\n",
    "    else:\n",
    "        print (p,q)\n",
    "        print (u'模型ARMA(%s,%s)符合白噪声检验' % (p,q))\n",
    "        break\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>ARMA Model Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>  <td>CWXT_DB:184:C:\\</td> <th>  No. Observations:  </th>     <td>42</td>    \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>            <td>ARMA(1, 0)</td>    <th>  Log Likelihood     </th>  <td>-593.325</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>css-mle</td>     <th>  S.D. of innovations</th> <td>324533.697</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>          <td>Tue, 14 Nov 2017</td> <th>  AIC                </th>  <td>1192.650</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>              <td>21:26:40</td>     <th>  BIC                </th>  <td>1197.863</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Sample:</th>           <td>10-01-2014</td>    <th>  HQIC               </th>  <td>1194.560</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th></th>                 <td>- 11-11-2014</td>   <th>                     </th>      <td> </td>    \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "            <td></td>               <th>coef</th>     <th>std err</th>      <th>z</th>      <th>P>|z|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>const</th>                 <td>  3.48e+07</td> <td> 3.59e+05</td> <td>   96.862</td> <td> 0.000</td> <td> 3.41e+07</td> <td> 3.55e+07</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>ar.L1.CWXT_DB:184:C:\\</th> <td>    0.8797</td> <td>    0.070</td> <td>   12.579</td> <td> 0.000</td> <td>    0.743</td> <td>    1.017</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<caption>Roots</caption>\n",
       "<tr>\n",
       "    <td></td>   <th>           Real</th> <th>         Imaginary</th> <th>         Modulus</th> <th>        Frequency</th>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>AR.1</th>               1.1367                    +0.0000j                    1.1367                    0.0000     \n",
       "</tr>\n",
       "</table>"
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                              ARMA Model Results                              \n",
       "==============================================================================\n",
       "Dep. Variable:        CWXT_DB:184:C:\\   No. Observations:                   42\n",
       "Model:                     ARMA(1, 0)   Log Likelihood                -593.325\n",
       "Method:                       css-mle   S.D. of innovations         324533.697\n",
       "Date:                Tue, 14 Nov 2017   AIC                           1192.650\n",
       "Time:                        21:26:40   BIC                           1197.863\n",
       "Sample:                    10-01-2014   HQIC                          1194.560\n",
       "                         - 11-11-2014                                         \n",
       "=========================================================================================\n",
       "                            coef    std err          z      P>|z|      [0.025      0.975]\n",
       "-----------------------------------------------------------------------------------------\n",
       "const                   3.48e+07   3.59e+05     96.862      0.000    3.41e+07    3.55e+07\n",
       "ar.L1.CWXT_DB:184:C:\\     0.8797      0.070     12.579      0.000       0.743       1.017\n",
       "                                    Roots                                    \n",
       "=============================================================================\n",
       "                 Real           Imaginary           Modulus         Frequency\n",
       "-----------------------------------------------------------------------------\n",
       "AR.1            1.1367           +0.0000j            1.1367            0.0000\n",
       "-----------------------------------------------------------------------------\n",
       "\"\"\""
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arma.summary() # 当p,q值为0，0时，summary方法报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 35581705.89246708,  35488222.8438646 ,  35405985.60720885,\n",
       "        35333641.33523785,  35269999.92533833])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "forecast_values, forecasts_standard_error, forecast_confidence_interval = arma.forecast(5)\n",
    "forecast_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "DatetimeIndex: 5 entries, 2014-11-12 to 2014-11-16\n",
      "Data columns (total 2 columns):\n",
      "实际值    5 non-null float64\n",
      "预测值    5 non-null float64\n",
      "dtypes: float64(2)\n",
      "memory usage: 120.0 bytes\n"
     ]
    }
   ],
   "source": [
    "predictdata = pd.DataFrame(xtest_value)\n",
    "predictdata.insert(1,'CWXT_DB:184:C:\\\\_predict',forecast_values)\n",
    "predictdata.rename(columns={'CWXT_DB:184:C:\\\\':u'实际值','CWXT_DB:184:C:\\_predict':u'预测值'},inplace=True)\n",
    "predictdata.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>实际值</th>\n",
       "      <th>预测值</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>COLLECTTIME</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2014-11-12</th>\n",
       "      <td>35704312.58</td>\n",
       "      <td>35581705.89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-11-13</th>\n",
       "      <td>35704980.73</td>\n",
       "      <td>35488222.84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-11-14</th>\n",
       "      <td>34570385.45</td>\n",
       "      <td>35405985.61</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-11-15</th>\n",
       "      <td>34673820.69</td>\n",
       "      <td>35333641.34</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-11-16</th>\n",
       "      <td>34793245.31</td>\n",
       "      <td>35269999.93</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                     实际值          预测值\n",
       "COLLECTTIME                          \n",
       "2014-11-12   35704312.58  35581705.89\n",
       "2014-11-13   35704980.73  35488222.84\n",
       "2014-11-14   34570385.45  35405985.61\n",
       "2014-11-15   34673820.69  35333641.34\n",
       "2014-11-16   34793245.31  35269999.93"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result_d = predictdata.applymap(lambda x: '%.2f' % x) # 将表格中各个浮点值都格式化\n",
    "result_d.to_excel('pedictdata_C_BIC_ARMA.xlsx')\n",
    "result_d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                   实际值        预测值\n",
      "COLLECTTIME                      \n",
      "2014-11-12   35.704313  35.581706\n",
      "2014-11-13   35.704981  35.488223\n",
      "2014-11-14   34.570385  35.405986\n",
      "2014-11-15   34.673821  35.333641\n",
      "2014-11-16   34.793245  35.270000\n",
      "0.462308002\n",
      "0.533460826783\n",
      "0.0132815193493\n",
      "误差阈值为1.5\n",
      "平均绝对误差为：0.4623, \n",
      "均方根误差为：0.5335, \n",
      "平均绝对百分误差为：0.0133\n",
      "误差检验通过！\n"
     ]
    }
   ],
   "source": [
    "# 第 * 5 * 步--D盘---------模型评价\n",
    "# 为了评价时序预测模型效果的好坏，本章采用3个衡量模型预测精度的统计量指标：平均绝对误差、均方根误差、平均绝对百分误差\n",
    "# -*- coding:utf-8 -*-\n",
    "import pandas as pd\n",
    "\n",
    "inputfile4 = 'pedictdata_C_BIC_ARMA.xlsx'\n",
    "result = pd.read_excel(inputfile4,index_col='COLLECTTIME')\n",
    "result = result.applymap(lambda x: x/10**6)\n",
    "print result\n",
    "\n",
    "# 计算误差\n",
    "abs_ = (result[u'预测值']-result[u'实际值']).abs()\n",
    "mae_ = abs_.mean() # mae平均绝对误差\n",
    "rmas_ = ((abs_**2).mean())**0.5 #rmas均方根误差\n",
    "mape_ = (abs_/result[u'实际值']).mean() #mape平均绝对百分误差\n",
    "# print abs_\n",
    "print mae_\n",
    "print rmas_\n",
    "print mape_\n",
    "errors = 1.5\n",
    "print '误差阈值为%s' % errors\n",
    "if (mae_ < errors) & (rmas_ < errors) & (mape_ < errors):\n",
    "    print (u'平均绝对误差为：%.4f, \\n均方根误差为：%.4f, \\n平均绝对百分误差为：%.4f' % (mae_, rmas_, mape_))\n",
    "    print '误差检验通过！'\n",
    "else:\n",
    "    print '误差检验不通过！'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
